<?php
/**
 * Created by PhpStorm.
 * User: wangchaozhen
 * Date: 2018/5/5
 * Time: 11:53
 */
namespace App\Service\Cron;

use App\Http\Controllers\Controller;
use App\Models\Indent\OrderModel;
use App\Service\Balance\BalanceService;
use App\Service\Customer\CustomerService;
use App\Service\Indent\OrderService;
use App\Service\Jpush\JpushService;
use App\Service\ServiceLog;
use App\Service\Work\BestWorksService;
use DB;

class StaticsCronService
{
    /**
     * 自动确认收货
     */
    public static function ConfirmOrder()
    {
        //一次获取10个超时未确认的订单
        $out_time = time()-259200;
        $out_orders = DB::select("select order_id,auid,order_type,uid,price,article_id from lsj_order 
where status=1 and paystatus=1 and price>0 
and order_id not in (select order_id from lsj_cron_fail_log where fail_type=1) 
and UNIX_TIMESTAMP(IFNULL(updated_at,created_at)) <= {$out_time} order by id  limit 5");

       if($out_orders){
           //存在超时订单处理
           foreach ($out_orders as $order_info){
               $order_id = $order_info->order_id;
               DB::beginTransaction();
               try{
                   $order_data = array(
                       'status' => 2,
                       'updated_at' => date("Y-m-d H:i:s")
                   );
                   $detail_data = array(
                       'order_id' => $order_id,
                       'remark' => 5, //确认收货
                       'order_type' => $order_info->order_type,
                       'created_at' => date("Y-m-d H:i:s")
                   );
                   $balance_data = array(
                       'uid' => $order_info->auid,
                       'relation_uid' =>  $order_info->uid,
                       'order_id' => $order_id,
                       'money' => $order_info->price,
                       'title' => '商品收入',
                       'type' => 2,
                       'article_id' => $order_info->article_id,
                       'created_at' => date('Y-m-d H:i:s'),
                   );

                   OrderService::changeOrder($order_id,$order_data);
                   OrderService::addOrderDetail($detail_data);
                   BalanceService::addBalanceLog($balance_data);
                   //商家增加余额
                   CustomerService::incBalance($order_info->auid,$order_info->price);
                   DB::commit();
               }catch (\Exception $e){
                   DB::rollback();
                   //插入失败日志中
                   $data = array(
                       'fail_id' => $order_info->order_id,
                       'fail_type' => 1,
                       'created' => time()
                   );
                   DB::table('lsj_cron_fail_log')->insert($data);

                   $error_string = "订单号:".$order_id."\n";
                   $error_string .= "异常文件:".$e->getFile()."\n";
                   $error_string .= "异常行号".$e->getLine()."\n";
                   $error_string .= "ERROR:".$e->getMessage()."\n";
                   $error_string .= $e->getTraceAsString()."\n";
                   ServiceLog::record("confirm_order_fail.txt",$error_string,"order");
                   continue;
               }

               //发送买卖消息
               $ext = Controller::getSendExt('sj_00000000000005');

               $article_info = BestWorksService::findArticle([
                   'id' => $order_info->article_id
               ]);
               $message = $article_info['title'].'商品,买家已确认收货!';
               $ext['article_id'] = $order_info->article_id;
               $ext['title'] = $article_info['title'];
               $ext['push_class'] = 'pay';
               $ext['content'] = $message;
               JpushService::pushMessage($order_info->auid,$message,$ext);
           }
       }
    }
}